package de.intarsys.security.smartcard.smartcardio;

import java.security.Security;
import java.util.List;

import javax.smartcardio.Card;
import javax.smartcardio.CardChannel;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.CardTerminals;
import javax.smartcardio.CardTerminals.State;
import javax.smartcardio.CommandAPDU;
import javax.smartcardio.ResponseAPDU;
import javax.smartcardio.TerminalFactory;

public class Connect {

	public static void main(String[] args) {
		try {
			new Connect().run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void connect(CardTerminal cardTerminal) throws Exception {
		System.out.println("" + cardTerminal + " connect " + cardTerminal.getName());
		Card card = cardTerminal.connect("*");
		try {
			CardChannel channel = card.getBasicChannel();
			System.out.println("ATR : " + card.getATR().toString());
			System.out.println("FB : " + card.getATR().getBytes()[0] + " > " + Integer.toHexString(card.getATR().getBytes()[0] ));
			System.out.println("FB : " + card.getATR().getBytes()[1] + " > " + Integer.toHexString(card.getATR().getBytes()[1] ));
//			  new byte[]{ 0x80, 0xb0, 0x00, 0x04, 0x02, 0x00, 0x0d }, 
//              new byte[]{ 0x00, 0xc0, 0x00, 0x01, 0x0d } // 00 c0 00 01 0d
	        
	        // read cmd  // Send SELECT/RESET command
	        byte[] rHeader = new byte[]{0x00, (byte) 0xA4, 0x04, 0x00, 0x08, (byte) 0xA0, 0x00, 0x00, 0x00, 0x54, 0x48, 0x00, 0x01};
	        CommandAPDU commandAPDU = new CommandAPDU(rHeader);
	        ResponseAPDU responseAPDU = channel.transmit(commandAPDU);
	        System.out.println("DATA rHeader : " + responseAPDU.getData().length);
	        
	        byte[] header = new byte[]{  (byte) 0x80, (byte) 0xb0, 0x00, 0x04, 0x02, 0x00, 0x0d };
	         commandAPDU = new CommandAPDU(header);
	         responseAPDU = channel.transmit(commandAPDU);
	        System.out.println("DATA header : " + responseAPDU.getData().length);
	        byte[] header2 = new byte[]{0x00, (byte) 0xc0, 0x00, 0x00, 0x0d};
	         commandAPDU = new CommandAPDU(header2);
	         responseAPDU = channel.transmit(commandAPDU);
	        System.out.println("DATA header2 : " + responseAPDU.getData().length);
	       
	        System.out.println();
	        System.out.println(new String(responseAPDU.getData(),"UTF-8"));
	        

			
		} finally {
			System.out.println("" + card + " disconnect");
			card.disconnect(false);
		}
	}

	public void run() throws Exception {
		Security.insertProviderAt(new SmartcardioProvider(), 1);
		//
		TerminalFactory factory = TerminalFactory.getDefault();
		CardTerminals terminals = factory.terminals();
//		while (System.in.available() == 0) {
			List<CardTerminal> list = terminals.list(State.CARD_INSERTION);
			if (list.isEmpty()) {
				System.out.println("no terminals");
			}
			for (CardTerminal cardTerminal : list) {
				try {
					connect(cardTerminal);
				} catch (Exception e) {
					System.out.println("error connecting " + e);
				}
			}
//			System.out.println("wait for change");
//			if (terminals.waitForChange(1000)) {
//				System.out.println("change detected...");
//			} else {
//				System.out.println("timeout");
//			}
//		}
	}
}
